﻿app.controller('HomeController', ["$scope", "$firebaseArray", function ($scope, $firebaseArray) {
    $scope.greeting = "howdy from Home Controller"

    $scope.editMode = false; // FALSE == adding, else user is editing
    $scope.currIdOfElement = -1; // index (on array) of the element clicked 

    // FB ref
    var ref = new Firebase("https://315demo.firebaseio.com/fbtodo");

    // READ - bind
    $scope.fbTodo = $firebaseArray(ref);

    $scope.temp = {};
    $scope.temp.name = "";
    $scope.temp.task = "";

    // CREATE 
    $scope.doAdd = function () {
        console.log("name...", $scope.temp.name, "task...", $scope.temp.task);
        $scope.fbTodo.$add({
            name: $scope.temp.name,
            task: $scope.temp.task
        });
    }
    // UPDATE
    $scope.doEdit = function (tId) {
        $scope.currIdOfElement = tId;
        $scope.temp.name = $scope.fbTodo[tId].name;
        $scope.temp.task = $scope.fbTodo[tId].task;
        $scope.editMode = true;
    }
    $scope.saveEdit = function () {
        $scope.fbTodo[$scope.currIdOfElement].name = $scope.temp.name;
        $scope.fbTodo[$scope.currIdOfElement].task = $scope.temp.task;
        $scope.fbTodo.$save($scope.currIdOfElement);
        console.log("triedf saving...");
        $scope.editMode = false;
    }
    // DELETE 
    $scope.doDelete = function (dId) {
        $scope.fbTodo.$remove(dId);
    }


    /// placehoder data for Blog example....
    $scope.user = { name: "mike", address: "123 Elm St." }

    $scope.fbArr = [];

    var blogArticle = {
        id: 321,
        author: "smither",
        date: "3333",
        content: "fffs sdf sdfs; oijhasijfsaijfa ae;oijh sdfisd",
        blurb: "oihdfjosaidjf ",
        title: "first  post"
    }
    var blogArticle2 = {
        id: 322221,
        author: "smither",
        date: "3333",
        content: "fffs sdf sdfs; oijhasijfsaijfa ae;oijh sdfisd",
        blurb: "oihdfjosaidjf ",
        title: "second   post"
    }



    $scope.fbArr.push(blogArticle);
    $scope.fbArr.push(blogArticle2);

}])